<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="168px">
      <el-form-item label="折旧名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入折旧名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="月数" prop="months">
        <el-input
          v-model="queryParams.months"
          placeholder="请输入月份"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="折旧后剩余价值" prop="depreciation_min">
        <el-input
          v-model="queryParams.depreciation_min"
          placeholder="请输入折旧后剩余价值"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['depreciations:depreciations:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['depreciations:depreciations:edit']"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['depreciations:depreciations:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['admin:assets:assets:export']"
          type="primary"
          plain
          icon="el-icon-position"
          size="mini"
          :loading="exportLoading"
          @click="handleInput"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['depreciations:depreciations:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="depreciationsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="折旧名称" align="center" prop="name"  sortable />
      <el-table-column label="月数" align="center" prop="months"  sortable />
      <el-table-column label="折旧后剩余价值" align="center" prop="depreciation_min"  sortable />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['depreciations:depreciations:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['depreciations:depreciations:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改vadmin/assets/对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="折旧名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入折旧名称" />
        </el-form-item>
        <el-form-item label="月数" prop="months">
          <el-input v-model="form.months" placeholder="请输入月份" />
        </el-form-item>
        <el-form-item label="折旧后剩余价值" prop="depreciation_min">
          <el-input v-model="form.depreciation_min" placeholder="请输入折旧后剩余价值" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <InputFile ref="inputfile" @inputFile="inputFile"></InputFile>
  </div>
</template>

<script>
import {
  listDepreciations,
  getDepreciations,
  delDepreciations,
  addDepreciations,
  updateDepreciations,
  exportDepreciations,
  importsDepreciations
} from "@/api/vadmin/basic/depreciations";
import InputFile from "@/views/components/InputFile";

export default {
  name: "Depreciations",
  components: { InputFile },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // vadmin/assets/表格数据
      depreciationsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        months: null,
        depreciation_min: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "折旧名称不能为空", trigger: "blur" }
        ],
        months: [
          { required: true, message: "月份不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleInput() {
      this.$refs.inputfile.open();
    },
    inputFile(val) {
      // 是否更新已经存在的用户数据
      importsDepreciations(val).then(response => {
        this.$alert("导入成功！", "导入结果", { dangerouslyUseHTMLString: true });
        this.getList();
      });
    },
    /** 查询vadmin/assets/列表 */
    getList() {
      this.loading = true;
      listDepreciations(this.queryParams).then(response => {
        this.depreciationsList = response.data.results;
        this.total = response.data.count;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        description: null,
        modifier: null,
        dept_belong_id: null,
        update_datetime: null,
        create_datetime: null,
        name: null,
        deleted_datetime: null,
        months: null,
        depreciation_min: null,
        creator_id: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加折旧数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getDepreciations(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改折旧数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDepreciations(this.form).then(response => {
              this.$message({
                message: "修改成功",
                type: "success"
              });
              this.open = false;
              this.getList();
            });
          } else {
            addDepreciations(this.form).then(response => {
              this.$message({
                message: "新增成功",
                type: "success"
              });
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        delDepreciations(ids).then(res => {
          this.getList();
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        });
      }).catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除"
        });
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有数据?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return exportDepreciations(queryParams);
      }).then(response => {
        this.download(response.data.file_url, response.data.name);
      });
    }
  }
};
</script>
